import {
    setOneLevelPartitions,
    setBanners,
    setRankingVideos,
    setPartitions,
    setLiveData,
    setLiveList,
    setShouldLoad,
    setRoomData,
    setRankingPartitions,
    setUpUserInfo,
    setVideoinfo
} from './actions'
import {
    getRoomInfo,
    getPlayUrl,
    getBanner,
    getPartitions,
    getRankings,
    getLiveIndexData,
    getLiveListData,
    getRankingPartitions,
    getUserInfo,
    getLiveUrl,
    getVideoInfo
} from 'api/base'
import { parse } from 'query-string'
const typeList = [
    '电台',
    '视频唱见',
    '第五人格',
    '王者荣耀',
    '网游',
    '手游',
    '单机',
    '娱乐',
    '绘画'
]
export function getIndexContent() {
    return async (dispatch, getState) => {
        let { data } = await getPartitions()
        let oneLevels = dealTypes(data[0])
        let { data: list } = await getBanner()
        let banner = dealBanner(list)
        let { data: rank } = await getRankings(0)
        let videos = dealVideo(rank.list)
        dispatch(setOneLevelPartitions(oneLevels))
        dispatch(setBanners(banner))
        dispatch(setRankingVideos(videos))
    }
}
export function getPartitionList() {
    return async (dispatch, getState) => {
        let { data = [] } = await getPartitions()
        let parts = Dealpart(data)
        parts = parts.filter((e) => [13, 23, 11, 177].indexOf(e.id) === -1)
        dispatch(setPartitions(parts))
    }
}
export function getLiveData() {
    return async (dispatch, getState) => {
        let {
            data: { module_list }
        } = await getLiveIndexData()
        const bannerList = module_list.find((item) => item.module_info.title === 'banner位').list

        const itemModules = module_list.filter((e) => {
            return typeList.includes(e.module_info.title)
        })
        const itemList = itemModules.map((e) => {
            const query = parse(e.module_info.link.split('?')[1])

            const temp = {
                title: e.module_info.title,
                parentAreaId: query.parent_area_id,
                parentAreaName: query.parent_area_name,
                areaId: query.area_id,
                areaName: query.area_name,
                list: []
            }
            temp.list = DealLive(e.list.slice(0, 4))
            return temp
        })
        dispatch(
            setLiveData({
                bannerList,
                itemList
            })
        )
    }
}
export function getUser(id) {
    return async (dispatch) => {
        let { data } = await getUserInfo(id)
        let user = {
            mId: data.mid,
            name: data.name,
            face: data.face,
            level: data.level,
            sex: data.sex,
            sign: data.sign,
            following: data.status.following,
            follower: data.status.follower
        }
        dispatch(setUpUserInfo(user))
    }
}
export function getLiveListInfo(obj) {
    return async (dispatch) => {
        let { data } = await getLiveListData(obj)
        let list = DealLive(data.list)
        dispatch(
            setLiveList({
                total: data.count,
                list
            })
        )
        dispatch(setShouldLoad(false))
    }
}
export function getRankingList(rid) {
    return (dispatch) => {
        return Promise.all([getRankingPartitions(), getRankings(rid)]).then(([res1, res2]) => {
            let parts = DealRankPart(res1.data)
            let videos = dealVideo(res2.data.list).slice(0, 30)
            dispatch(setRankingPartitions(parts))
            dispatch(setRankingVideos(videos))
        })
    }
}
export function getRoomData(roomId) {
    return (dispatch) => {
        const promises = [getRoomInfo({ roomId }), getLiveUrl({ roomId })]
        return Promise.all(promises).then(([res1, res2]) => {
            const data = res1.data
            let live = {
                title: data.title,
                roomId: data.room_id,
                onlineNum: data.online,
                cover: data.user_cover,
                isLive: data.live_status,
                playUrl: '',
                upUser: null
            }
            let temp = res2.data
            live.playUrl = temp.durl[temp.current_quality - 1].url
            dispatch(
                setRoomData({
                    parentAreaId: data.parent_area_id,
                    parentAreaName: data.parent_area_name,
                    areaId: data.area_id,
                    areaName: data.area_name,
                    uId: data.uid,
                    description: data.description,
                    liveTime: data.live_time,
                    live
                })
            )
        })
    }
}
export function getVideoDetail(aId) {
    return async (dispatch) => {
        let { data } = await getVideoInfo(aId)
        let video = {
            aId: data.aid,
            title: data.title,
            pic: data.pic,
            desc: data.desc,
            playCount: data.stat.view,
            barrageCount: data.stat.danmaku,
            publicDate: data.pubdate,
            duration: data.duration,
            cId: data.cid,
            url: '',
            owner: {
                mId: data.owner.mid || 0,
                name: data.owner.name,
                face: data.owner.face || 0
            },
            twoLevel: { id: data.tid, name: data.tname }
        }
        let { data: list } = await getPlayUrl(aId, video.cId)
        video.url = list.durl[0].url
        dispatch(setVideoinfo(video))
    }
}
function DealRankPart(data) {
    return data.map((item) => {
        return {
            id: item.tid,
            name: item.typename
        }
    })
}
export function DealLive(data) {
    return data.map((e) => {
        return {
            title: e.title,
            roomId: e.roomid,
            onlineNum: e.online,
            cover: e.cover,
            isLive: 0,
            playUrl: '',
            upUser: {
                mId: 0,
                name: e.uname,
                face: e.face
            }
        }
    })
}
function Dealpart(data) {
    if (data) {
        let ret = []
        const firstTypes = data[0] || []
        if (firstTypes) {
            ret = firstTypes.map((e) => {
                let { tid: id, typename: name } = e
                const children = dealTypes(data[id])
                return { id, name, children }
            })
        }
        return ret
    }
}

export function dealTypes(data) {
    return data
        .map((e) => {
            if (![13, 23, 11, 177].includes(e.tid)) {
                return { id: e.tid, name: e.typename }
            }
        })
        .filter(Boolean)
}
function dealBanner(data) {
    if (!data) return []
    return data.map((e) => {
        return {
            id: e.id,
            name: e.name,
            pic: e.pic,
            url: e.url
        }
    })
}
export function dealVideo(data) {
    return data.map((e) => {
        return {
            aId: parseInt(e.aid, 10),
            title: e.title,
            pic: e.pic,
            desc: e.desc,
            playCount: e.play,
            barrageCount: e.video_review,
            publicDate: e.pubdate || 0,
            duration: e.duration,
            cId: e.pubdate || 0,
            url: e.url || '',
            owner: {
                mId: e.mid || 0,
                name: e.author,
                face: e.face || 0
            }
        }
    })
}
export function dealUser(data) {
    return data.map((item) => {
        return {
            mId: item.mid,
            name: item.uname,
            face: item.upic,
            level: item.level,
            sex: '',
            sign: item.usign,
            following: 0,
            follower: item.fans,
            videoCount: item.videos || 0
        }
    })
}
